#--------------------------------------------------------------------------------------------------------------------#
#----------------------------------------------- Portmann & Stojanovic ----------------------------------------------#
#--------------------------------------------------- November 2020 --------------------------------------------------#
#------- Are Immigrant-Origin Candidates Penalized in Virtue of Ingroup Favoritism or Outrgoup Hostility? -----------#
#--------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------- Descriptive overview ---------------------------------------------#
#--------------------------------------------------------------------------------------------------------------------#

# ------------------------------------------------------------------ Read data

rm(list=ls())
setwd(".../...")
load("dat_all")
# Data includes all candidates, also those from cantons who use de facto majority rule and the canton of Aargau 
#that did not provide data on negative and positive preference votes
               
load("d_nr15_cand")

# ------------------------------------------------------------------ Table 1: Despcriptive overview N party lists, candidates and ballots by canton

overview_cantons_listscand <- d_nr15_cand %>% 
dplyr::group_by(bfscantno.x) %>% 
dplyr::summarise(n_list = n_distinct(list_id), 
n_cand = n_distinct(cand_id))

overview_cantons_ballots <- d_nr15_cand %>% 
dplyr::group_by(bfscantno.x, list_id) %>% 
dplyr::summarize(mod_ballots = mean(bfsballots_changed),
unmod_ballots = mean(bfsballots_unchanged))

overview_cantons_ballots <- overview_cantons_ballots %>% 
dplyr::group_by(bfscantno.x) %>% 
dplyr::summarize(mod_ballots = sum(mod_ballots), 
unmod_ballots = sum(unmod_ballots), 
perc_changed_ballots = (sum(mod_ballots)/(sum(unmod_ballots) + sum(mod_ballots)))*100)

overview_cantons <- cbind(overview_cantons_listscand, overview_cantons_ballots)
overview_cantons <- overview_cantons[, -c(4, 6)]

overview_cantons_sum <- overview_cantons %>% dplyr::select(c(2:5)) %>%
summarise_all(funs(sum))
overview_cantons_sum$perc_changed_ballots <- sum(d_nr15_cand$bfsballots_changed, na.rm = TRUE)/sum(d_nr15_cand$bfsballots_unchanged + d_nr15_cand$bfsballots_changed, na.rm = TRUE)
overview_cantons_sum$perc_changed_ballots <- overview_cantons_sum$perc_changed_ballots*100

overview_cantons_sum$perc_changed_ballots <- mean(d_nr15_cand$bfsballots_changed, na.rm = TRUE)/mean(d_nr15_cand$bfsballots_unchanged + d_nr15_cand$bfsballots_changed, na.rm = TRUE)
overview_cantons_sum$perc_changed_ballots <- overview_cantons_sum$perc_changed_ballots*100

overview_cantons <- merge(overview_cantons, overview_cantons_sum, all=TRUE)
overview_cantons$bfscantno.x <- ifelse(is.na(overview_cantons$bfscantno.x), 99, overview_cantons$bfscantno.x)

overview_cantons$bfscantno.x <- factor(overview_cantons$bfscantno.x, 
labels = c("Zurich", "Berne", "Lucerne", "Schwyz", "Zug", 
"Fribourg", "Solothurn", "Basel-City", "Basel-Country", "Schaffhausen",
"St. Gall", "Grissons", "Thurgau", "Ticino", "Vaud", "Valais", "Neuchatel", 
"Geneva", "Jura", "Total all cantons"))

overview_cantons$bfscantno.x <- as.character(overview_cantons$bfscantno.x)
overview_cantons <- overview_cantons[order(overview_cantons$bfscantno.x),]
overview_cantons <- overview_cantons[c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 16),]
overview_cantons <- overview_cantons[, c(5, 1, 2, 3, 4)]
overview_cantons$perc_changed_ballots <- as.character(round(overview_cantons$perc_changed_ballots, digits = 0))
overview_cantons$perc_changed_ballots <- paste0("(", overview_cantons$perc_changed_ballots, "%)")
overview_cantons$ballots = paste(overview_cantons$mod_ballots, overview_cantons$perc_changed_ballots, sep=" ")

overview_cantons <- overview_cantons[, -c(4, 5)]

colnames(overview_cantons) <- c("Canton","Party lists", "Candidates", "Modified ballots")
print(xtable(as.matrix(overview_cantons), caption = "Number of party lists, candidates, and modified ballots, by canton"), 
include.rownames=FALSE, caption.placement = "top")

# ------------------------------------------------------------------ Figure 1: Candidates and elected parliamentarians by cantons

# ----- Non-Swiss name

#Code f�r Ledigname
dat_all$nS_name_base <- NA
dat_all$nS_name_base <- ifelse(dat_all$bfsnamdoubl==1, dat_all$origname2_swissname, dat_all$origname1_swissname)
table(dat_all$nS_name_base)

#Nonswissname
dat_all$nonswissname <- NA
dat_all$nonswissname[dat_all$nS_name_base==0] <- 0
dat_all$nonswissname[dat_all$nS_name_base==1] <- 1
dat_all$nonswissname[dat_all$nS_name_base==2] <- 1

dat_all$Surname <- factor(dat_all$nonswissname, 
levels = c(0,1), 
labels = c("Non-Swiss", "Swiss"))

# ----- Remove cantons with only one seat in the National Council (majoroity voting system)

dat_all$bfscantno <- as.numeric(dat_all$bfscantno)
dat_all <- dat_all[!(dat_all$bfscantno == 4 | dat_all$bfscantno == 6 | dat_all$bfscantno == 7 | dat_all$bfscantno == 8 | dat_all$bfscantno == 15 | dat_all$bfscantno == 16) , ]

dat_all$canton <- factor(dat_all$bfscantno, 
levels = c(1, 2, 3, 5, 9, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26), 
labels = c("ZH", "BE", "LU", "SZ", "ZG", "FR", "SO", "BS", "BL", "SH", "SG", "GR", "AG", "TG", 
"TI", "VD", "VS", "NE", "GE", "JU"))

# ----- Make Figure

#Candidates
dat2 <- (dat_all %>% dplyr::count(canton, Surname) %>%  # Group by canton, then count number in each group
dplyr::mutate(pct=n/sum(n)))
dat2 <- dat2 %>% dplyr::group_by(canton) %>% dplyr::mutate(ypos = sum(n) + 25, label=(min(pct)/(min(pct)+max(pct)))) 

p1 <- ggplot((dat2),
aes(canton, n, fill= Surname)) +
geom_bar(stat="identity") +
geom_text(data=subset(dat2, pct < 0.5), aes(label=paste0(sprintf("%1.1f", 
label*100),"%"), y=ypos), size=3.5) + labs(x="") +
scale_fill_manual("Surname", values = c("gray40", "gray60"))  +
geom_text(x=11, y=850, label="Candidates", size=3.6, fontface="plain") +
theme_bw() + theme(plot.margin = unit(c(0.2,0.1,0,0.2), "cm")) + 
theme(axis.text.y= element_text(size = 9.5, colour = "black")) +
theme(axis.text.x= element_text(size = 9.5, colour = "black")) + 
labs(y="n candidates")
p1

#Elected
dat3 <- dat_all %>% dplyr::filter(bfselected == 1) %>% 
dplyr::count(canton, Surname) %>%
dplyr::mutate(pct = n/sum(n))

dat3 <- dat3 %>% dplyr::group_by(canton) %>% dplyr::mutate(ypos = sum(n) + 2, 
label=(min(pct)/(min(pct)+max(pct))))
dat3$label <- ifelse(dat3$label==0.5, 0, dat3$label)

p2 <- ggplot((dat3),
aes(canton, n, fill=Surname)) +
geom_bar(stat="identity") +
geom_text(data=subset(dat3, pct < 0.5), aes(label=paste0(sprintf("%1.1f", 
label*100),"%"), y=ypos), size=3.5) + 
labs(x="") + scale_fill_manual("Surname", values = c("gray40", "gray60")) +
geom_text(x=11, y=33, label="Elected represenatives", size=3.6, fontface="plain") +
theme_bw() + theme(plot.margin = unit(c(0,0.1,0.2,0.4), "cm")) + 
theme(axis.text.y= element_text(size = 9.5, colour = "black")) +
theme(axis.text.x= element_text(size = 9.5, colour = "black")) +
labs(y="n elected")
p2

grid_arrange_shared_legend <- function(..., nrow = 1, ncol = length(list(...)), 
position = c("bottom", "right")) {
  
plots <- list(...)
position <- match.arg(position)
g <- ggplotGrob(plots[[1]] + theme(legend.position = position))$grobs
legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]]
lheight <- sum(legend$height)
lwidth <- sum(legend$width)
gl <- lapply(plots, function(x) x + theme(legend.position = "none"))
gl <- c(gl, nrow = nrow, ncol = ncol)
  
combined <- switch(position,
"bottom" = arrangeGrob(do.call(arrangeGrob, gl),
legend, ncol = 1,
heights = unit.c(unit(1, "npc") - lheight, lheight)),
"right" = arrangeGrob(do.call(arrangeGrob, gl),
legend, ncol = 2,
widths = unit.c(unit(1, "npc") - lwidth, lwidth)))
grid.newpage()
grid.draw(combined)
  
}

figure1 <- grid_arrange_shared_legend(p1, p2, nrow = 2, ncol = 1)

jpeg(file="figure1.jpeg", 
width = 8, height = 5, units = "in", res = 300)
#plot(figure1)
dev.off()

# ------------------------------------------------------------------ Table 2: Name origins of candidates and elected representatives

#Ledig name generieren
dat_all$origin_name_base <- NA
dat_all$origin_name_base <- ifelse(dat_all$bfsnamdoubl==1, dat_all$origname2_code, dat_all$origname1_code)
table(dat_all$origin_name_base)

#Recode Bangladesh from Southeastern/Eastern Asian (9) to Southern Asian (8)
table(dat_all$origin_name_base)
dat_all$origin_name_base <- ifelse(dat_all$origin_name_base == 9 & dat_all$origname1_txt=="Bangladesh", 8, dat_all$origin_name_base)

#Change categories
table(dat_all$origin_name_base)
dat_all$origin_name_base <- mapvalues(dat_all$origin_name_base, 
                                   from = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 99), 
                                   to = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 8, 11, 12, 99))

#Table
T3a <- dat_all %>% dplyr::ungroup() %>%
  dplyr::select(origin_name_base) %>% 
  dplyr::group_by(origin_name_base) %>%
  dplyr::summarize(n_candidates=n()) %>%
  dplyr::mutate(x=if_else(origin_name_base==1, 14, 0)) %>% # Add cantons with de facto majority voting right (they fielded 15 candidates who all have Swiss names)
  dplyr::mutate(n_candidates=n_candidates + x) %>%
  dplyr::mutate(freq_candidates = (n_candidates / sum(n_candidates))*100)

T3b <- dat_all %>% dplyr::ungroup() %>%
  dplyr::select(origin_name_base, bfselected) %>% 
  dplyr::filter(bfselected==1) %>%
  dplyr::group_by(origin_name_base) %>%
  dplyr::summarise(n_elected=n()) %>%
  dplyr::mutate(x=if_else(origin_name_base==1, 6, 0)) %>% 
  dplyr::mutate(n_elected=n_elected + x) %>%
  dplyr::mutate(freq_elected = (n_elected / sum(n_elected))*100)

T3c <- merge(x = T3a, y = T3b, by.x = "origin_name_base", by.y = "origin_name_base", all.x = TRUE)
T3c[is.na(T3c)] <- 0

T3d <- T3c %>% dplyr::filter(origin_name_base!=1) %>% dplyr::select(c(2:7)) %>%
  summarise_all(funs(sum))

T3 <- merge(T3c, T3d, all=TRUE)

table(T3$origin_name_base)
T3$origin_name_base <- factor(T3$origin_name_base, 
                              levels = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 99), 
                              labels = c("Swiss", "Hispanic", "Eastern European", "Southern European",
                                         "Yugoslavian", "Albanian", "Western European/Nordic/Anglo", 
                                         "Central- and South Asian", "East- and Southeast Asian",
                                         "Arabic", "Turkish/Kurdish", "(Other) African", "Total non-Swiss", "Unknown"))

T3 <- T3[c(12, 11, 10, 9, 8, 7, 6, 5, 4, 1, 2, 3, 13, 14), ]

x <- c("origin_name_base", "n_candidates", "freq_candidates", "n_elected", "freq_elected")
T3 <- T3[x]

T3$freq_candidates <- round(T3$freq_candidates, digits = 2)
T3$freq_elected <- round(T3$freq_elected, digits = 2)

colnames(T3) <- c("Name origin", "n candidates", "% candidates", "n elected", "% elected")

print(xtable(T3, digits=c(0, 0,0,2,0,2)), include.rownames=FALSE)

rm(list = c("T3a", "T3b", "T3c", "T3d", "x"))
